package com.mysen.sso.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mysen.model.Pagenation;
import com.mysen.model.Response;
import com.mysen.sso.cache.SessionCache;
import com.mysen.sso.model.Permission;
import com.mysen.sso.model.User;
import com.mysen.sso.service.PermissionService;

@Controller
public class PermissionController {
	
	@Reference
	private PermissionService service;
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private SessionCache sessionCache;
	
	@PostMapping("/permission")
	@ResponseBody
	public Response<Permission> save(@RequestBody Permission permission){
		Response<Permission> resp = new Response<Permission>();
		User curUser = sessionCache.getUser(request);
		permission.setCuser(curUser.getUserId());
		
		resp.setSuccess(true);
		resp.setData(service.save(permission));
		return resp;
	}
	
	@PostMapping("/permissions")
	@ResponseBody
	public Response<?> queryByPage(@RequestBody Permission permission){
		Pagenation page = permission.getPage();
		if(page == null) {
			page = new Pagenation(0, 10);
			permission.setPage(page);
		}
		
		return service.selectByPage(permission);
	}
	
	@PutMapping("/permission/{id}")
	@ResponseBody
	public Response<Permission> updatePermission(@RequestBody Permission permission, @PathVariable("id") String permissionId){
		Response<Permission> resp = new Response<Permission>();
		permission.setPermissionId(permissionId);
		service.updateById(permission);
		resp.setData(permission);
		resp.setSuccess(true);
		return resp;
	}

	@GetMapping("/permission/{id}")
	@ResponseBody
	public Response<Permission> getPermission(@PathVariable("id") String permissionId){
		Response<Permission> resp = new Response<Permission>();
		resp.setData(service.selectById(permissionId));
		resp.setSuccess(true);
		return resp;
	}
	
	@DeleteMapping("/permission/{id}")
	@ResponseBody
	public Response<Permission> delete(@PathVariable("id") String permissionId){
		Response<Permission> resp = new Response<Permission>();
		service.deleteById(permissionId);
		resp.setSuccess(true);
		return resp;
	}

	@GetMapping("/permissions")
	@ResponseBody
	public Response<?> getRoleTree(@RequestParam("pId") String pId, @RequestParam("isAll") boolean isAll){
		List<Permission> tree = service.selectTree(pId, isAll);
		Response<?> resp = new Response<>();
		resp.setDatas(tree);
		resp.setSuccess(true);
		return resp;
	}
}
